from django.db import models
from sync.models import SFObject,SFField
from django.db.models import Q
from django.db import connection, transaction
from django.template import RequestContext
from django.shortcuts import render_to_response


class View(models.Model):
    obj=models.ForeignKey(SFObject)
    vtype=models.CharField(max_length=55,default="single") #single,multi,form,custom
    name=models.CharField(max_length=255,null=True)
    custome_html=models.TextField()
    custome_query=models.TextField()
    
    class Meta:
        db_table = 'wizard_view'
        app_label = 'wizard'
    
   
    """
    View using functions
    """
    def response(self,request,dev=False):
        if not dev:
            if not self.has_permission(request):
                return self.no_perm()
        return self.resp(request)
    def resp(self,request,dev):
        return None
    def has_permission(self,request):
        #TODO: check users's permission
        return True
    def no_perm(self,request):
        return render_to_response('wizard/no_permission.html', 
                              {'name':self.name},
                              context_instance = RequestContext(request))
    def not_found(self,request):
        return render_to_response('wizard/not_found.html', 
                              {'name':self.name,
                               },
                              context_instance = RequestContext(request))
    """
    View editing using wizard
    """
    def set_fields(self,request):
        from wizard.models.view_field import ViewField
        fields= SFField.objects.filter(obj=self.obj,is_local=True)
        for f in fields:
            f_on=request.POST.get(f.local_field,False)
            if f_on:
                ViewField.create_field(self,f)
    def update_field_list(self):
        from wizard.models import ViewField
        #fields that are not in view
        sql="""SELECT * FROM sync_sf_field a
            LEFT JOIN wizard_view_field b
            ON a.id = b.sf_field_id  
            WHERE ISNULL (b.id) AND a.obj_id=%d"""%self.obj.pk
        #add this fields to view
        sf_fields=SFField.row(sql)
        for f in sf_fields:
            vf = ViewField(view=self,sf_field=f)
            vf.save()
        sql="""SELECT * FROM wizard_view_field a
            LEFT JOIN sync_sf_field b
            ON b.id = b.sf_field_id  
            WHERE ISNULL (b.id) AND a.view_id=%d"""%self.pk
        #add this fields to view
        view_fields=ViewField.row(sql)
        for vf in view_fields:
            vf.in_schema=False
            vf.save()
    def get_edit_fields(self):
        """
        option 1 
        All object fields must be defined as view fields 
        only used fields will be defined as active
        pros. 
        if you delete a field and return it again you have all settings ready
        a single function call to make sure your list of fields is updated
        A simple function to get all fields - active or not. 
        
         
        #option 1
        self.update_field_list()
        from wizard.models import ViewField
        return ViewField.objects.filter(view=self)
 
        option 2 
        viewFields are defined only when used 
        """
        from wizard.models import ViewField
        return ViewField.objects.filter(view=self)
    def get_object_fields(self):
        return SFField.objects.filter(obj=self.obj,is_local=True)
        
